home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / challenge / 12.04-Apr96 / LifeTestCode DR3.sit / Life TestCode DR3 / LifeWindow.c < prev    next >
Text File  |  1996-04-03  |  6KB  |  230 lines

  1. #define JRB 0
  2. #define SHOWGRID 0
  3.  
  4. /* LifeWindow.c    */
  5. /* ©Ludovic Nicolle 1996    */
  6.  
  7. #include "LifeWindow.h"
  8. extern Boolean    gDrawGrid;
  9. extern Boolean    gToroidLike;
  10. extern short    gToroidLimit;
  11.  
  12. void DisplayCells(GrafPtr lifeC, WindowPtr screenC, Rect *screenR)
  13. {
  14.     Point    lifeSize, screenSize, hvRatio;
  15.     short    gRatio;
  16.     Rect    lifeRect, outRect, lifetor, screentor;
  17.     short    i;
  18.     GrafPtr    savePort;
  19.     
  20.     GetPort(&savePort);
  21.     SetPort(screenC);
  22.     if (lifeC->portBits.baseAddr == nil)
  23.     {
  24.         EraseRect(screenR);
  25.         SetPort(savePort);    
  26.         return;
  27.     }
  28.     lifeSize.h = lifeC->portBits.bounds.right - 
  29.                     lifeC->portBits.bounds.left;
  30.     lifeSize.v = lifeC->portBits.bounds.bottom - 
  31.                     lifeC->portBits.bounds.top;
  32.     screenSize.h = screenR->right - screenR->left;
  33.     screenSize.v = screenR->bottom - screenR->top;
  34.     
  35.     gRatio = GiveRatio(lifeC, screenR, &hvRatio);
  36.     if (gRatio == 0)
  37.     {/*we don't have enough place to draw the entire bitmap*/
  38.         if ((hvRatio.v == 0 ) && (hvRatio.h == 0))
  39.         {
  40.             lifeRect = *screenR;
  41.             OffsetRect(&lifeRect, lifeRect.left, lifeRect.top);
  42.             outRect = *screenR;
  43.         } else
  44.         {    /*have to clip only the direction where  ratio = 0*/
  45.             lifeRect = lifeC->portBits.bounds;
  46.             lifeRect.bottom = lifeRect.top + 
  47.                         (!hvRatio.v ? screenSize.v:lifeSize.v);
  48.             lifeRect.right = lifeRect.left + 
  49.                         (!hvRatio.h ? screenSize.h:lifeSize.h);
  50.             outRect = lifeRect;
  51.             OffsetRect(&outRect, screenR->left, screenR->top);
  52.         }
  53.     } else
  54.     {
  55.         SetRect(&outRect, screenR->left, screenR->top,
  56.                 screenR->left + gRatio * lifeSize.h,
  57.                 screenR->top + gRatio * lifeSize.v);
  58.         lifeRect = lifeC->portBits.bounds;
  59.     }
  60.     OffsetRect(&outRect, gRatio, gRatio);
  61.  
  62.     CopyBits(&lifeC->portBits, &((GrafPtr)screenC)->portBits,
  63.                 &lifeRect, &outRect, srcCopy, screenC->visRgn);    
  64.     
  65.     if ((gRatio > gToroidLimit) && gToroidLike)
  66.     {
  67.         lifetor = lifeRect;
  68.         lifetor.top = lifetor.bottom - 1;
  69.         screentor = outRect;
  70.         screentor.top = outRect.top - gRatio;
  71.         screentor.bottom = outRect.top;
  72.         CopyBits(&lifeC->portBits, &((GrafPtr)screenC)->portBits,
  73.                 &lifetor, &screentor, srcCopy, screenC->visRgn);    
  74.  
  75.         lifetor = lifeRect;
  76.         lifetor.bottom = lifetor.top + 1;
  77.         screentor = outRect;
  78.         screentor.top = outRect.bottom;
  79.         screentor.bottom = outRect.bottom + gRatio;
  80.         CopyBits(&lifeC->portBits, &((GrafPtr)screenC)->portBits,
  81.                 &lifetor, &screentor, srcCopy, screenC->visRgn);    
  82.  
  83.         outRect.top -= gRatio;
  84. //        outRect.top -= 1;
  85.         outRect.bottom += gRatio;
  86. //        outRect.bottom += 1;
  87.         
  88.         lifetor = outRect;
  89.         lifetor.left = lifetor.right - gRatio;
  90.         screentor = outRect;
  91.         screentor.left = outRect.left - gRatio;
  92.         screentor.right = outRect.left;
  93.         CopyBits(&((GrafPtr)screenC)->portBits, 
  94.                 &((GrafPtr)screenC)->portBits,
  95.                 &lifetor, &screentor, srcCopy, screenC->visRgn);    
  96.  
  97.         lifetor = outRect;
  98.         lifetor.right = lifetor.left + gRatio;
  99.         screentor = outRect;
  100.         screentor.left = outRect.right;
  101.         screentor.right = outRect.right + gRatio;
  102.         CopyBits(&((GrafPtr)screenC)->portBits, 
  103.                 &((GrafPtr)screenC)->portBits,
  104.                 &lifetor, &screentor, srcCopy, screenC->visRgn);    
  105.  
  106. /*    if I decide to add other feature later. Unuseful now    */        
  107.         outRect.top += gRatio;
  108. //        outRect.top += 1;
  109.         outRect.bottom -= gRatio ;
  110. //        outRect.bottom -= 1;
  111.     }
  112.  
  113.     InsetRect(&outRect, -1, -1);
  114.     FrameRect(&outRect);
  115.     InsetRect(&outRect, 1, 1);
  116.     
  117.     if ((gRatio> gToroidLimit) && gDrawGrid) {
  118.         for (i=1; i<=lifeSize.h; ++i) {
  119.             MoveTo(outRect.left + i*gRatio, outRect.top);    
  120.             Line(0,gRatio*lifeSize.v);
  121.         }
  122.         for (i=1; i<=lifeSize.v; ++i) {
  123.             MoveTo(outRect.left, outRect.top + i*gRatio);
  124.             Line(gRatio*lifeSize.h,0);
  125.         }
  126.     }
  127.  
  128.     SetPort(savePort);
  129. }
  130.  
  131.  
  132. void ClickSet(GrafPtr lifeC, WindowPtr screenC, Rect *screenR, 
  133.                             Point *clickPt, short *ratio)
  134. {
  135.     Point    lifeSize, screenSize, clickPos, hvRatio;
  136.     Rect    lifeRect, r;
  137.     GrafPtr    savePort;
  138.     short    gRatio;
  139.     Point    topleftP;
  140.     
  141.     GetPort(&savePort);
  142.     gRatio = GiveRatio(lifeC, screenR, &hvRatio);
  143. /* zut    */
  144.     lifeSize.h = lifeC->portBits.bounds.right - 
  145.                     lifeC->portBits.bounds.left;
  146.     lifeSize.v = lifeC->portBits.bounds.bottom - 
  147.                     lifeC->portBits.bounds.top;
  148.     screenSize.h = screenR->right - screenR->left;
  149.     screenSize.v = screenR->bottom - screenR->top;
  150.     if (gRatio == 0)
  151.     {/*we don't have enough place to draw the entire bitmap*/
  152.         if ((hvRatio.v == 0 ) && (hvRatio.h == 0))
  153.         {
  154.             lifeRect = *screenR;
  155.         } else
  156.         {    /*have to clip only the direction where  ratio = 0*/
  157.             lifeRect = lifeC->portBits.bounds;
  158.             lifeRect.bottom = lifeRect.top + 
  159.                         (!hvRatio.v ? screenSize.v:lifeSize.v);
  160.             lifeRect.right = lifeRect.left + 
  161.                         (!hvRatio.h ? screenSize.h:lifeSize.h);
  162.             OffsetRect(&lifeRect, screenR->left, screenR->top);
  163.         }
  164.     } else
  165.     {
  166.         SetRect(&lifeRect, screenR->left, screenR->top,
  167.                 screenR->left + gRatio * lifeSize.h,
  168.                 screenR->top + gRatio * lifeSize.v);
  169.     }
  170.     OffsetRect(&lifeRect, gRatio, gRatio);
  171.  
  172. /*    zut    */
  173.     
  174.     if (!PtInRect(*clickPt, &lifeRect))
  175.         return;
  176.     topleftP.h = lifeRect.left;
  177.     topleftP.v = lifeRect.top;
  178.     
  179.     if (gRatio == 0)
  180.         gRatio = 1;    /* allow clickset on visible parts
  181.                             of large bitmap*/
  182.     
  183.     clickPos.h = (clickPt->h - topleftP.h + 0) / gRatio;
  184.     clickPos.v = (clickPt->v - topleftP.v + 0) / gRatio;
  185.     SetPort(lifeC);
  186.     SetRect(&lifeRect, clickPos.h, clickPos.v, 
  187.                     clickPos.h + 1, clickPos.v + 1);            
  188.     InvertRect(&lifeRect);
  189.     
  190.  
  191.     SetPort(screenC);
  192.     SetRect(&r,(clickPos.h)*gRatio,(clickPos.v)*gRatio,
  193.                 (clickPos.h+1)*gRatio, (clickPos.v+1)*gRatio);
  194.     OffsetRect(&r,topleftP.h,topleftP.v);
  195.     InvertRect(&r);
  196.     InvalRect(&r);
  197.     if ((clickPos.h == lifeC->portBits.bounds.left) ||
  198.         (clickPos.h == lifeC->portBits.bounds.right - 1) ||
  199.         (clickPos.v == lifeC->portBits.bounds.top) ||
  200.         (clickPos.v == lifeC->portBits.bounds.bottom - 1))
  201.     {
  202.         InvalRect(screenR);
  203.     }
  204.  
  205.     SetPort(savePort);
  206.     SetPt(clickPt, topleftP.h + gRatio * clickPos.h,
  207.                     topleftP.v + gRatio * clickPos.v);
  208.     *ratio = gRatio;
  209. }
  210.  
  211.  
  212. short GiveRatio(GrafPtr lifeC, Rect *screenR, Point *hvRatio)
  213. {
  214.     Point    lifeSize, screenSize;
  215.     
  216. /* we add 2 to the cells bitmap size to allow the display
  217. of the opposite band of data    */
  218.     lifeSize.h = lifeC->portBits.bounds.right - 
  219.                     lifeC->portBits.bounds.left + 2;
  220.     lifeSize.v = lifeC->portBits.bounds.bottom - 
  221.                     lifeC->portBits.bounds.top + 2;
  222.     screenSize.h = screenR->right - screenR->left;
  223.     screenSize.v = screenR->bottom - screenR->top;
  224.     
  225.     hvRatio->v = screenSize.v / lifeSize.v;
  226.     hvRatio->h = screenSize.h / lifeSize.h;
  227.     return((hvRatio->v > hvRatio->h) ? hvRatio->h : hvRatio->v);
  228. }
  229.  
  230.